package com.ant.myagile

import com.ant.myagile.backlog.UserStory

class UserStoryService {

	def findUserStoryByProject(pid) {
		Project p = Project.findById(pid)
		return p.userStories;
	}

	def informToMemberWhenDelete(userDelete,userStoryID) {
		HashSet<Integer> emails= new HashSet<Integer>()

		def userStory = UserStory.get(userStoryID)
		def project= userStory.project

		def team_projects= Team_Project.findAll() {
			project == project
		}
		for(team_project in team_projects) {
			def team_members= Team_Member.findAll() {
				team == team_project.team
			}
			for(team_member in team_members) {
				emails.add(team_member.member.username)
			}
		}

		MailService m=new MailService()
		def text="User story #"+userStory.id+" has been removed by <b>"+userDelete+"</b>."
		text+="<hr>"
		text+="<h3>UserStory #"+userStory.id+": "+userStory.name+"</h3>"
		text+="<ul>"
		text+="<li>Name: "+userStory.name+"</li>"
		text+="<li>Description: "+userStory.description+"</li>"
		text+="<li>Value: "+userStory.value+"</li>"
		text+="<li>Status: "+userStory.status+"</li>"
	    text+="</ul>"
		text+="<hr>"
		text+="<p>You have received this notification because you have either subscribed to it, or are involved in it.</p>"

		for(email in emails)
		{
					m.sendMail("Inform about remove user story", text, email)
					print "Send email to " + email
		} 

	}
}
